package dk.itu.samd.group2.tagapastry;
import java.io.IOException;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.Id;
import rice.p2p.commonapi.IdFactory;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;

public class Tag {

	public Tag(IdFactory fac, String keyword, String type) {
		init(fac,keyword,type,"");
	}
	
	public Tag(IdFactory fac, String keyword, String type, String extension) {
		init(fac,keyword,type,extension);
	}
	
	/*
	 * @param keyword = "news" 
	 * 
	 */
	private void init(IdFactory fac, String keyword, String type, String extension) {
		this.keyword = keyword.toLowerCase().trim();
		this.type = type.toLowerCase().trim();
		this.extension = extension;
		
		this.id = fac.buildId(this.keyword + this.type + this.extension);
		setAlternateName("");
	}

	public Tag(InputBuffer buf, Endpoint endpoint) throws IOException {
		this.keyword = buf.readUTF();
		this.type = buf.readUTF();
		this.extension = buf.readUTF();
		this.id = endpoint.readId(buf, buf.readShort());
		
		this.isComplex = buf.readBoolean();
		setAlternateName(buf.readUTF());
	}

	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeUTF(this.keyword);
		buf.writeUTF(this.type);
		buf.writeUTF(this.extension);
		buf.writeShort(this.id.getType());
		
		this.id.serialize(buf);
		
		buf.writeBoolean(this.isComplex);
		buf.writeUTF(getAlternateName());
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).
		append(keyword).
		append(type).
		append(extension).
		toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (obj == null)
			return false;
		if (obj.getClass() != getClass())            
			return false;
		
		Tag otherTag = (Tag)obj;
		
		return new EqualsBuilder().
		append(keyword, otherTag.keyword)
		.append(type, otherTag.type)
		.append(extension, otherTag.extension).
		isEquals();
	}

	// TODO should have package vis
	public Id getId() {
		return id;
	}

	public String getKeyword() {
		return keyword;
	}

	public String getType() {
		return type;
	}
	
	public String getExtension() {
		return extension;
	}

	public String toString() {
		return "{name: " + keyword + ", type: " + type + ", extension: " + extension + "}";
	}
	
	public String getAlternateName() {
		return this.altName;
	}
	
	public void setAlternateName(String altName) {
		this.altName = altName;
	}
	
	public boolean isComplex() {
		return this.isComplex;
	}
	
	public void makeComplex() {
		this.isComplex = true;
	}
	
	/*
	 * 
	 */
	public int getSize() {
		return keyword.length() + type.length() + extension.length() + altName.length()
			+ 1 + id.getByteArrayLength();
	}
	
	private String keyword;
	private String type;
	private Id id;
	private String extension;
	private String altName;
	private boolean isComplex;
	
	
	public char getExtensionLastChar() {
		return extension.charAt(extension.length() - 1);
	}
}
